#include "shopinffroom.h"
#include "ui_shopinffroom.h"
#include<fstream>
#include<iostream>
#include<QDebug>
shopinffroom::shopinffroom(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::shopinffroom)
{
    ui->setupUi(this);
    //reqpost = new Getinftonet(this);
}

shopinffroom::~shopinffroom()
{
    qDebug()<<"del shopinfo";
    delete ui;
}

void shopinffroom::init(QString isonl,Getinftonet *reqpos ){

islog = isonl;
reqpost = reqpos;
}

void shopinffroom::on_pushButton_clicked()
{
    if(haveimg == false) return; //已存在图片

    filepath = "";
    filepath = file->getOpenFileName(this, tr("Open File"),
                                     "/home",
                                     tr("Images (*.png *.xpm *.jpg)"));
    if(filepath == "") return;
    ui->pushButton->setStyleSheet(QString("border-image:url(%1)").arg(filepath));
    haveimg = false;
}

void shopinffroom::on_pushButton_set_clicked()
{
    structshopinf shopinf;
    QFile file(filepath);
    file.open(QIODevice::ReadOnly);
    shopinf.ShopName=ui->ShopName->text();
    shopinf.ip="";
    shopinf.IsLogin=islog;
    shopinf.text=ui->text->toPlainText();  //--
    shopinf.PhoneNumber=ui->PhoneNumber->text();
    shopinf.shoper=ui->shoper->text();
    shopinf.fdstype=ui->fdstype->text();
    shopinf.addr=ui->addr->text();//--



    shopinf.photopath =file.readAll();
   // char photos[6000];
    std::fstream ifs;
//    ifs.open(filepath.toStdString(), std::ios::in | std::ios::binary);
//    ifs.read(photos, 6000);

  //  shopinf.photopath.append(photos);

{ /* 获取shoptyp 早餐、午餐、晚餐？*/
        shopinf.shoptyp.clear();
     if(ui->checkBox1->isChecked()) shopinf.shoptyp.append("1");
        else shopinf.shoptyp.append("0");
     if(ui->checkBox2->isChecked()) shopinf.shoptyp.append("1");
        else shopinf.shoptyp.append("0");
     if(ui->checkBox3->isChecked()) shopinf.shoptyp.append("1");
        else shopinf.shoptyp.append("0");
    }
    // 数据打包
    QByteArray buf;
    buf.append(QString("{"
               "\"unit\":\"%1\","
               "\"ShopName\":\"%2\","
               "\"IsLogin\":\"%3\","
               "\"addr\":\"%4\","
                "\"PhoneNumber\":\"%5\","
               "\"shoper\":\"%6\","
               "\"shoptyp\":\"%7\","
               "\"fdstype\":\"%8\","
                "\"text\":\"%9\","
                "\"imgtype\":\"jpg\","
               "\"length\":\"%10\",").arg(shopunit)
               .arg(shopinf.ShopName).arg(shopinf.IsLogin).arg(shopinf.addr)
               .arg(shopinf.PhoneNumber).arg(shopinf.shoper).arg(shopinf.shoptyp)
               .arg(shopinf.fdstype).arg(shopinf.text)
               .arg(QString::number(shopinf.photopath.length()))
               );

    buf.append("\"photo\":\"");
    buf+=shopinf.photopath;
    buf.append("\"}");

reqpost->topost("http://hunya.fun:9000/cliupshopinf",buf); //？？
file.close();
sleep(2);
reqpost->getshopinftosend();

this->close();
delete this;
}

void shopinffroom::on_pushButton_ch_clicked()
{
    haveimg = true;
    emit ui->pushButton->clicked();
}
